window.onload = function () {
    var name = sessionStorage.getItem("name");
    $('.cName').val(name)
    var sex = sessionStorage.getItem("sex");
    if (sex == '男') {
        $($("input[name='radio2']")[1]).attr('checked', '1')
    } else {
        $($("input[name='radio2']")[0]).attr('checked', '0')
    }
    var phone = sessionStorage.getItem("phone");
    $('.cPhone').val(phone)
    // 动态添加教育经历
    e = 1;
    $(".education").click(function () {
        let ret = ($(".deteleBox").length);
        if (ret == 3) {
            $.toast("教育经历最多只能添加三个");
            return false
        }
        $(".educationBox").append('<div class="deteleBox">\n' +
            '              <div class="experImgDetele eduDelete">\n' +
            '                <img src="./img/detele.svg" alt="">\n' +
            '              </div>\n' +
            '              <div style="height: 39px;background: #f3f3f3"></div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd"><label for="" class="weui_label">开始时间</label></div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input my-input" name="jytime" id="kaishi' + e + '" type="text" value="">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd"><label for="" class="weui_label">结束时间</label></div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input my-input" name="jytime" id="end' + e + '" type="text" value="">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd">\n' +
            '                  <label class="weui_label">毕业院校全称</label>\n' +
            '                </div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input" name="jytime" type="text" placeholder="请输入毕业院校">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd">\n' +
            '                  <label class="weui_label">专业</label>\n' +
            '                </div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input" name="jytime" type="text" placeholder="请输入专业">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd">\n' +
            '                  <label class="weui_label">学历及学位</label>\n' +
            '                </div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input educa" name="jytime" id="educa' + e + '" type="text" placeholder="-- 请选择 --">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '            </div>');
        e = e + 1;
    });
    // 删除教育经历
    $(document).on('click', '.eduDelete', function () {
        $(this).parent().remove();
    });
    // 动态添加工作经历
    n = 1;
    $(".work").click(function () {
        let ret = ($(".deteleBox1").length);
        if (ret == 3) {
            $.toast("工作经历最多只能添加三个");
            return false
        }
        $(".workBox").append('<div class="deteleBox deteleBox1">\n' +
            '              <div class="experImgDetele workDelete">\n' +
            '                <img src="./img/detele.svg" alt="">\n' +
            '              </div>\n' +
            '              <div style="height: 39px;background: #f3f3f3"></div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd"><label for="" class="weui_label">开始时间</label></div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input my-input" name="jltime" id="my-input' + n + 1 + '" type="text" value="">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd"><label for="" class="weui_label">结束时间</label></div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input my-input" name="jltime" id="my-input' + n + 2 + '" type="text" value="">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd">\n' +
            '                  <label class="weui_label">工作单位全称</label>\n' +
            '                </div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input outfocus" name="jltime" type="text" placeholder="请输入工作单位">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd">\n' +
            '                  <label class="weui_label">部门及职位</label>\n' +
            '                </div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input outfocus" name="jltime" type="text" placeholder="请输入部门及职位">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd">\n' +
            '                  <label class="weui_label">离职原因</label>\n' +
            '                </div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input outfocus" name="jltime" type="text" placeholder="请填写离职原因">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd">\n' +
            '                  <label class="weui_label">证明人</label>\n' +
            '                </div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input outfocus" name="jltime" type="text" placeholder="请输入姓名">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '              <div class="weui_cell">\n' +
            '                <div class="weui_cell_hd">\n' +
            '                  <label class="weui_label">电话</label>\n' +
            '                </div>\n' +
            '                <div class="weui_cell_bd weui_cell_primary">\n' +
            '                  <input class="weui_input outfocus verify" id="zmrPhone' + (n + 1) + '" name="jltime" type="text" placeholder="请输入电话号码">\n' +
            '                </div>\n' +
            '              </div>\n' +
            '            </div>');
        n = n + 1;
    });
    // 删除工作经历
    $(document).on('click', '.workDelete', function () {
        $(this).parent().remove();
    });
    // 验证正则表达式
    $(document).on('blur', '.verify', function (){
        var id = $(this).attr("id");
        var pattern = $("#" + id).val();
        // 手机
        if (id === 'phone' || id === 'zmrPhone' || id === 'zmrPhone' + n) {
            if (pattern === '' || pattern && /^1[3|4|5|8]\d{9}$/.test(pattern)) {
            } else {
                $.toptip('手机号码输入错误', 'error');
                return false
            }
        }
        // 身份证
        if (id === 'pattern') {
            // var pattern = $("#" + id).val();
            if (pattern === '' || pattern && /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(pattern)) {
            } else {
                $.toptip('身份证号码输入错误', 'error');
                return false
            }
        }
        // 邮箱
        if (id === 'email') {
            if (pattern === '' || pattern && /^\w+@\w+(.[a-zA-Z]{2,3}){1,2}$/.test(pattern)) {
            } else {
                $.toptip('邮箱输入错误', 'error');
                return false
            }
        }
    });
    // 民族
    $("#nation").picker({
        title: "-- 请选择 --",
        cols: [
            {
                textAlign: 'center',
                values: [
                    "汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族", "满族", "侗族",
                    "瑶族", "白族", "土家族", "哈尼族", "哈萨克族", "傣族", "黎族", "僳僳族", "佤族", "畲族", "高山族", "拉祜族",
                    "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族", "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族",
                    "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族", "俄罗斯族", "鄂温克族",
                    "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族",
                ]
            }
        ]
    });
    // 籍贯
    $("#native").cityPicker({
        title: "-- 请选择 --"
    });
    // 学历
    $(document).on('touchstart', '.educa', function () {
        var id = $(this).attr("id");
        $("#" + id).picker({
            title: "-- 请选择 --",
            cols: [
                {
                    textAlign: 'center',
                    values: ["初中及以下", "高中", "中专", "技校/职高", "大专", "本科", "硕士", "博士及以上", "其他"]
                }
            ]
        });
    })
    // 日历
    $(document).on('touchstart', '.my-input', function () {
        var id = $(this).attr("id");
        $("#" + id).datetimePicker({
            title: '-- 请选择日期 --',
            min: "1990-12-12",
            max: "2070-12-12 12:12",
            times: function () {
                return [];
            },
            onChange: function (picker, values, displayValues) {
            }
        });
        $(".outfocus").blur();
    });
    // 离焦事件
    $(document).on('touchstart', '.choice', function () {
        $(".outfocus").blur();
    });
    // 提交
    $(document).on('click', '#btnSub', function () {
        let name = $('.cName').val()
        let sex = $("input[type='radio'][name='radio2']:checked").val()
        let phone = $('.cPhone').val()
        let email = $('.cEmail').val()
        let cJiguan = $('.cJiguan').val()
        let cMinzu = $('.cMinzu').val()
        let cShengri = $('.cShengri').val()
        if(name == ''|| name == null || name == undefined) {
            $.alert('姓名不能为空!', '温馨提示');
            return false;
        }
        if(phone == ''|| phone == null || phone == undefined) {
            $.alert('手机号不能为空!', '温馨提示');
            return false;
        }
        if(email == ''|| email == null || email == undefined) {
            $.alert('邮箱不能为空!', '温馨提示');
            return false;
        }
        var time = [];
        var img_url = [];
        var tempfrom = {}
        var tempfrom1 = {}
        var tempfrom2 = {}
        $("input[name='jytime']").each(function () {
            img_url.push($(this).val());
        })
        var brr1 = [];
        var brr2 = [];
        var brr3 = [];
        if (img_url.length == 5) {
            for (var i = 0; i < img_url.length; i++) {
                brr1[i] = img_url[i];
            }
        }
        if (img_url.length == 10) {
            for (var i = 0; i < img_url.length / 2; i++) {
                brr1[i] = img_url[i];
                brr2[i] = img_url[i + img_url.length / 2];
            }
        }
        if (img_url.length == 15) {
            for (var i = 0; i < img_url.length / 3; i++) {
                brr1[i] = img_url[i];
                brr2[i] = img_url[i + img_url.length / 3];
                brr3[i] = img_url[i + img_url.length / 15 * 10];
            }
        }
        tempfrom["start_date"] = brr1[0]
        tempfrom["end_date"] = brr1[1]
        tempfrom["school_name"] = brr1[2]
        tempfrom["major"] = brr1[3]
        tempfrom["degree_name"] = brr1[4]

        tempfrom1["start_date"] = brr2[0]
        tempfrom1["end_date"] = brr2[1]
        tempfrom1["school_name"] = brr2[2]
        tempfrom1["major"] = brr2[3]
        tempfrom1["degree_name"] = brr2[4]

        tempfrom2["start_date"] = brr3[0]
        tempfrom2["end_date"] = brr3[1]
        tempfrom2["school_name"] = brr3[2]
        tempfrom2["major"] = brr3[3]
        tempfrom2["degree_name"] = brr3[4]
        time.push(tempfrom)
        time.push(tempfrom1)
        time.push(tempfrom2)

        var time1 = [];
        var img_url1 = [];
        var edufrom = {}
        var edufrom1 = {}
        var edufrom2 = {}
        $("input[name='jltime']").each(function () {
            img_url1.push($(this).val());
        })
        var edu1 = [];
        var edu2 = [];
        var edu3 = [];
        if (img_url1.length == 7) {
            for (var i = 0; i < img_url1.length; i++) {
                edu1[i] = img_url1[i];
            }
        }
        if (img_url1.length == 14) {
            for (var i = 0; i < img_url1.length / 2; i++) {
                edu1[i] = img_url1[i];
                edu2[i] = img_url1[i + img_url1.length / 2];
            }
        }
        if (img_url1.length == 21) {
            for (var i = 0; i < img_url1.length / 3; i++) {
                edu1[i] = img_url1[i];
                edu2[i] = img_url1[i + img_url1.length / 3];
                edu3[i] = img_url1[i + img_url1.length / 21 * 14];
            }
        }
        edufrom["start_date"] = edu1[0]
        edufrom["end_date"] = edu1[1]
        edufrom["company_name"] = edu1[2]
        edufrom["job_name"] = edu1[3]
        edufrom["leave_reason"] = edu1[4]
        edufrom["prove_username"] = edu1[5]
        edufrom["prove_mobile"] = edu1[6]

        edufrom1["start_date"] = edu2[0]
        edufrom1["end_date"] = edu2[1]
        edufrom1["company_name"] = edu2[2]
        edufrom1["job_name"] = edu2[3]
        edufrom1["leave_reason"] = edu2[4]
        edufrom1["prove_username"] = edu2[5]
        edufrom1["prove_mobile"] = edu2[6]

        edufrom2["start_date"] = edu3[0]
        edufrom2["end_date"] = edu3[1]
        edufrom2["company_name"] = edu3[2]
        edufrom2["job_name"] = edu3[3]
        edufrom2["leave_reason"] = edu3[4]
        edufrom2["prove_username"] = edu3[5]
        edufrom2["prove_mobile"] = edu3[6]
        time1.push(edufrom)
        time1.push(edufrom1)
        time1.push(edufrom2)

        $.modal({
            title: "温馨提示",
            text: "请再次检查信息，确认资料属实、无误！",
            buttons: [
                {
                    text: "确定",
                    onClick: function () {
                        let key = '195a9b99ea7a0d259151b5c412bb631b';
                        $.ajax({
                            type: "post",
                            url: "http://hplqytest.suoluomei.cn/index.php?s=/Hr/Index/resumeSave",
                            data: {
                                api_key: key,
                                name: name,
                                sex: sex,
                                mobile: phone,
                                email: email,
                                native_place: cJiguan,
                                nation: cMinzu,
                                birth: cShengri,
                                study: time,
                                work: time1
                            },
                            success: function (result) {
                                $.toast("提交成功");
                                $("input[type='text']").val('')
                                $("input[type='number']").val('')
                                $("input[type='email']").val('')
                            },
                            error: function () {
                                $.toast("提交失败");
                            }
                        });
                    }
                },
                {
                    text: "取消",
                    className: "default",
                    onClick: function () {
                    }
                },
            ]
        });
        return false;
    });

}